package org.alvin.i18n.ui;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.base.Strings;
import com.google.common.collect.Sets;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;

public class TrimeI18n {

    public static void main(String[] args) throws IOException {
//        JSONObject aobj = loadData("D:\\i18n\\trim\\in\\i18ncode-en.js");
//        JSONObject bobj = loadData("D:\\i18n\\trim\\in\\i18ncode-zh.js");
//
//        trimKeys(aobj, bobj);
////        trimKeys(bobj, aobj);
//
////        insertAttr(aobj, bobj);
////        insertAttr(bobj, aobj);
//
//        outData("D:\\i18n\\trim\\out\\i18ncode-en.js", aobj);
////        outData("D:\\i18n\\trim\\out\\i18ncode-zh.js", bobj);

//        trimA();
//        trimB();

//        insertAttrA();
//        insertAttrB();

/////////////////////////////////
//        JSONObject aobj = loadData("D:\\i18n\\trim\\app\\in\\home-en.ts");
//        JSONObject bobj = loadData("D:\\i18n\\trim\\app\\in\\home-zh.ts");
//        trimKeys(aobj, bobj);
//
//        outData("D:\\i18n\\trim\\app\\out\\home-en.ts", aobj);
        ///////////////////

//        /////////////////////////////////
//        JSONObject aobj = loadData("D:\\i18n\\trim\\app\\in\\home-zh.ts");
//        JSONObject bobj = loadData("D:\\i18n\\trim\\app\\out\\home-en.ts");
//        trimKeys(aobj, bobj);
//
//        outData("D:\\i18n\\trim\\app\\out\\home-zh.ts", aobj);
//        ///////////////////
//        /////////////////////////////////
//        JSONObject aobj = loadData("D:\\i18n\\trim\\app\\out\\home-zh.ts");
//        JSONObject bobj = loadData("D:\\i18n\\trim\\app\\out\\home-en.ts");
//        insertAttr(aobj, bobj);
//        outData("D:\\i18n\\trim\\app\\lastout\\home-zh.ts", aobj);
//        ///////////////////

        /////////////////////////////////
        JSONObject aobj = loadData("D:\\i18n\\trim\\app\\out\\home-en.ts");
        JSONObject bobj = loadData("D:\\i18n\\trim\\app\\lastout\\home-zh.ts");
        insertAttr(aobj, bobj);
        outData("D:\\i18n\\trim\\app\\lastout\\home-en.ts", aobj);
        ///////////////////
    }

    public static void insertAttrA() throws IOException {
        JSONObject aobj = loadData("D:\\i18n\\trim\\out2\\i18ncode-en.js");
        JSONObject bobj = loadData("D:\\i18n\\trim\\out2\\i18ncode-zh.js");

        insertAttr(aobj, bobj);
        outData("D:\\i18n\\trim\\lastout\\i18ncode-en.js", aobj);
    }

    public static void insertAttrB() throws IOException {
        JSONObject aobj = loadData("D:\\i18n\\trim\\out2\\i18ncode-zh.js");
        JSONObject bobj = loadData("D:\\i18n\\trim\\lastout\\i18ncode-en.js");

        insertAttr(aobj, bobj);
        outData("D:\\i18n\\trim\\lastout\\i18ncode-zh.js", aobj);
    }

    public static void trimA() throws IOException {
//        JSONObject aobj = loadData("D:\\i18n\\trim\\in\\i18ncode-en.js");
//        JSONObject bobj = loadData("D:\\i18n\\trim\\in\\i18ncode-zh.js");
//        trimKeys(aobj, bobj);
//        outData("D:\\i18n\\trim\\out\\i18ncode-en.js", aobj);
    }

    public static void trimB() throws IOException {
//        JSONObject aobj = loadData("D:\\i18n\\trim\\in\\i18ncode-zh.js");
//        JSONObject bobj = loadData("D:\\i18n\\trim\\out\\i18ncode-en.js");
//        trimKeys(aobj, bobj);
//        outData("D:\\i18n\\trim\\out\\i18ncode-zh.js", aobj);
    }

    public static void outData(String path, JSONObject jsonObject) throws IOException {
        System.out.println("输出结果" + path);
        Path realPath = Paths.get(path);
        String outString = JSON.toJSONString(jsonObject, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteDateUseDateFormat);
        Files.write(realPath, outString.getBytes(StandardCharsets.UTF_8));
    }

    public static JSONObject loadData(String path) throws IOException {
        byte[] buff = Files.readAllBytes(Paths.get(path));
        String text = new String(buff, StandardCharsets.UTF_8);
        int index = text.indexOf("{");
        return JSONObject.parseObject(text.substring(index));
    }

    public static void insertAttr(JSONObject aobj, JSONObject bobj) {
        for (String key : aobj.keySet()) {
            JSONObject aSubItem = aobj.getJSONObject(key);
            JSONObject bSubItem = bobj.getJSONObject(key);
            trimKeys(aSubItem, bSubItem);
        }
    }

    public static void trimKeys(JSONObject aobj, JSONObject bobj) {
        Set<String> allKeys = Sets.newHashSet();
        allKeys.addAll(aobj.keySet());
        allKeys.addAll(bobj.keySet());
        //
        allKeys.removeAll(aobj.keySet());
        System.out.println("多出来的后台：" + allKeys);
        for (String key : allKeys) {
            if (Strings.isNullOrEmpty(key)) {
                continue;
            }
            System.out.println(key + "==" + bobj.get(key));
            aobj.put(key, bobj.get(key));
        }
    }
}
